//
//  LGCancerTreatRecordHeader.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/10.
//

#import "LGCancerTreatRecordCell.h"
#import "LGCheckPhotoTool.h"
@interface LGCancerTreatRecordCell()
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UIView *bottomLine;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**  不良反应 */
@property (nonatomic , strong)UILabel *effectLabel;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , assign)NSInteger count;
/**   */
@property (nonatomic , strong)NSDictionary *keyDic;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGCancerTreatRecordCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.count = isPad?6:3;
        self.photoW = (Screen_W-viewPix(78)-viewPix(10)*(self.count-1))/self.count;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    // 1-手术、 2-放疗、 3-化疗、 4-靶向治疗、 5-免疫治疗、 6-内分泌治疗、 7-中医治疗、 8-介入治疗、 9-其他治疗
    NSString *type = LGNSString(dataDic[@"treat_type"]);
    self.titleLabel.text = LGNSString(dataDic[@"treat_type_name"]);
    self.dateLabel.text = [NSString stringWithFormat:@"治疗日期 %@",dataDic[@"treat_date"]];
    CGFloat baseHeight = 0;
    NSString *content = @"";
    NSString *title = @"";
    if ([type isEqualToString:@"1"]) {
        //手术
        baseHeight = 0;
        content = @"";
        
    }else if ([type isEqualToString:@"2"]){
        //放疗
        title = @"放疗部位";
        content = LGNSString(dataDic[@"radiotherapy_site"]);
        
    }else if ([type isEqualToString:@"7"] || [type isEqualToString:@"8"]){
        //中医 、 介入   没有药品
        title = @"";
        content = @"";
        
    }else{
        title = @"治疗药品";
        content = LGNSString(dataDic[@"medicine_name"]);
    }

    CGFloat contentTop = (content.length>0)?viewPix(12):0;
    self.contentLabel.text = (content.length>0)?[NSString stringWithFormat:@"%@ %@",title,content]:@"";
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(contentTop);
    }];
    
    NSString *effect = LGNSString(dataDic[@"syptoms"]);
    CGFloat effectTop = (effect.length>0)?viewPix(10):0;
    self.effectLabel.text = (effect.length>0)?[NSString stringWithFormat:@"不良反应  %@",effect]:@"";
    [self.effectLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(effectTop);
    }];
    
    
    //图片
    NSArray *files = dataDic[@"files"];
    CGFloat photoTop = (files.count>0)?viewPix(8):0;
    CGFloat photoH = (files.count/self.count)*(self.photoW+viewPix(10));
    photoH = (files.count%self.count)>0?(photoH+self.photoW):photoH;
    photoH = (files.count>0)?photoH+viewPix(30):photoH;
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.effectLabel.mas_bottom).offset(photoTop);
        make.height.equalTo(@(photoH));
    }];
    
    
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        UIImageView *imageView = [self viewWithTag: 1000+i];
        [imageView removeFromSuperview];
    }
    
  
    [self.itemArray removeAllObjects];
    
    for (NSInteger i=0; i<files.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.photoW+viewPix(10))*(i%self.count), (self.photoW+viewPix(10))*(i/self.count), self.photoW, self.photoW)];
        imageView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        imageView.tag = 1000+i;
        [self.photoView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:LGNSString(files[i][@"img_url"])]];
        [self.itemArray addObject:LGNSString(files[i][@"img_url"])];
    }
}

//查看详情
-(void)checkButtonAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkTreatRecord:)]) {
        [self.delegate checkTreatRecord:self.dataDic];
    }
}

-(void)checkPhotoAction:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.photoView];
    NSInteger index = point.x/(self.photoW+viewPix(10));
    if (index<self.itemArray.count) {
        LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        photoView.picArray = self.itemArray;
        [[ObjectTool mainWindow] addSubview:photoView];
        [photoView showViewAnimation:CGPointMake(0, 0) index:index];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.topLine];
    [self.contentView addSubview:self.bottomLine];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.dateLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.effectLabel];
    [self.baseView addSubview:self.photoView];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(10));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.width.height.equalTo(@(viewPix(10)));
    }];
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(self.iconView.mas_top);
        make.centerX.equalTo(self.iconView);
        make.width.equalTo(@(1));
    }];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom);
        make.bottom.equalTo(self.contentView);
        make.centerX.width.equalTo(self.topLine);
    }];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(4));
        make.left.equalTo(self.contentView).offset(viewPix(36));
        make.right.equalTo(self.contentView).offset(-viewPix(12));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.lessThanOrEqualTo(self.dateLabel.mas_left).offset(-viewPix(10));
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.titleLabel);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(12));
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.dateLabel);
    }];
    [self.effectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(10));
        make.left.right.equalTo(self.contentLabel);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.effectLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.contentLabel);
        make.height.equalTo(@(self.photoW));
        make.bottom.equalTo(self.baseView).offset(-viewPix(12));
    }];
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc]init];
        _topLine.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _topLine;
}

-(UIView *)bottomLine{
    if(!_bottomLine){
        _bottomLine = [[UIView alloc]init];
        _bottomLine.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _bottomLine;
}


-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"时间轴-手术治疗"]];
    }
    return _iconView;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#D7D3D3" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.clipsToBounds = YES;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _dateLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}


-(UILabel *)effectLabel{
    if(!_effectLabel){
        _effectLabel = [UILabel lableWithText:@"" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _effectLabel;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc]init];
        [_photoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkPhotoAction:)]];
    }
    return _photoView;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

@end
